import { _decorator, Component, Label, Node } from 'cc';
import { main } from './main';
const { ccclass, property } = _decorator;

@ccclass('GameManager')
export class GameManager extends Component {
    @property({ type: main })
    private main: main | null = null;
    @property({ type: Node })
    private mask: Node | null = null;
    @property({ type: Label })
    private label: Label | null = null;
    @property({ type: Label })
    private title: Label | null = null;
    @property({ type: Node })
    private operatorLayout: Node | null = null;

    private isStop = false;

    start() {
        this.main.setGameOverCallback(() => {
            this.gameOverCallback();
        })
    }

    update(deltaTime: number) {

    }

    //暂停游戏
    stopGame() {
        if (this.mask != null && this.label != null) {
            this.label.string = "继续游戏"
            this.isStop = true;
            this.mask.active = true;
            this.main.gameStop();
        }
    }

    //重新开始游戏
    gameReset() {
        this.main.initData()
    }

    //开始游戏
    startGame() {
        if (this.mask != null) {
            //分为游戏第一次开启，和中途暂停开启
            if (this.isStop) {
                this.main.gameProceed();
            } else {
                //点击开始按钮后，隐藏遮罩层，开始游戏               
                this.main.initData();
            }
            if (this.operatorLayout != null) this.operatorLayout.active = true;
            this.mask.active = false;
        }
    }

    //游戏结束回调
    gameOverCallback(): void {
        if (this.mask != null) {
            if (this.title != null) this.title.string = "游戏结束"
            if (this.label != null) this.label.string = "重新开始"
            this.mask.active = true;
            if (this.operatorLayout != null) this.operatorLayout.active = false;
            //点击重新开始按钮，游戏应该为第一次开启
            this.isStop = false;
        }
    }
}


